import webApi from "../webApi";
import {Notice} from "iview"
import notice from "../components/notice";

let userModule = {

    namespaced: true,

    state: {
        userName: "",
        userPassword: "",
        isLogined: false
    },
    mutations: {
        SignIn(state, { userName, userPassword }){
            state.userName = userName
            state.userPassword = userPassword

            state.isLogined = true
        },
        SignOut(state){
            state.userName = ""
            state.userPassword = ""
            state.isLogined = false
        }
    },
    actions: {
        async SignIn(context, { userName, userPassword }){
            return new Promise(async (resolve, reject) => {
                try {
                    let result = await webApi.signIn(userName, userPassword)
                    context.commit("SignIn", {
                        userName: result.signInName,
                        userPassword: result.signInPassword
                    })
                    notice.signInSuccess()
                    resolve(result)
                }
                catch (exception) {
                    notice.checkAndShowServerError(exception)
                    notice.signInError()
                    reject(exception)
                }
            })
        },
        SignOut(context){
            notice.signOutSuccess()
            context.commit("SignOut")
        }
    }
}

export default userModule;